package com.cxb.demotest.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;


@Configuration
@MapperScan(basePackages = "com.cxb.demotest.dao.mapper_one.**", sqlSessionFactoryRef = "oneSqlSessionFactory")
public class DataSourceOneConfig {
  @Value("${spring.datasource.druid.driver-class-name}")
  String driverClass;
  @Value("${spring.datasource.druid.url}")
  String url;
  @Value("${spring.datasource.druid.username}")
  String userName;
  @Value("${spring.datasource.druid.password}")
  String passWord;

  @Primary
  @Bean(name = "oneDataSource")
  @ConfigurationProperties("spring.datasource.one")
  public DataSource masterDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(driverClass);
    dataSource.setUrl(url);
    dataSource.setUsername(userName);
    dataSource.setPassword(passWord);
    return dataSource;
  }

  @Bean(name = "oneSqlSessionFactory")
  public SqlSessionFactory sqlSessionFactory(@Qualifier("oneDataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);
    sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
        .getResources("classpath:mybatis/mapper-one/*.xml"));

    return sessionFactoryBean.getObject();
  }
  @Bean(name = "oneSqlSessionTemplate")
  public SqlSessionTemplate sqlSessionFactoryTemplate(@Qualifier("oneSqlSessionFactory")SqlSessionFactory sqlSessionFactory ) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
  }
}

